//
//  AppDelegate.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/8/31.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "AppDelegate.h"
#import "GlobalHeader.h"
#import <IQKeyboardManager.h>
#import "IAALoginVC.h"
#import <YYWebImage.h>
#import "XDLocalPush.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "IAARTMsgQuery.h"

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self setGlobalAppearance];
    // 键盘
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    // 设置图片缓存时长
    YYImageCache *cache = [YYWebImageManager sharedManager].cache;
    cache.diskCache.ageLimit = 30 * 24 * 60 * 60;   // 30天
    // 请求推送的权限、iOS10推送的代理
    [XDLocalPush registerLocalPush];
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
    }
    // Background Fetch
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    
    // 测试代码
//    self.token = @"92460a06055b4c819dade8c3c180cd38";
//    self.organizeId = @"20041";
//    self.userNo = @"888888";
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - background fetch

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    self.completionHandler = completionHandler;
    //这里进行处理
    [[IAARTMsgQuery sharedInstance] requestRTMsgQuery];
    
}

#pragma mark - 推送相关代理
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//    / Required - 注册 DeviceToken
//    [JPUSHService registerDeviceToken:deviceToken];
    NSLog(@"注册成功返回token %@ ", deviceToken);
}
//
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//    // Required,For systems with less than or equal to iOS6
////    [JPUSHService handleRemoteNotification:userInfo];
//}
//
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//    NSLog(@"收到推送了%@", userInfo);
//    
////    [self processJPush:userInfo];
//    
//    // 当应用在内存中存活时，收到推送会激发该回调，在这里来处理。
//    // 后台时才会改变icon的数量，正在使用时不会。目前只需要清空icon数量标记。
//    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
//    
//    // IOS 7 Support Required
////    [JPUSHService handleRemoteNotification:userInfo];
////    completionHandler(UIBackgroundFetchResultNewData);
//}
//
//- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//    //Optional
//    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
//}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"%s, %@", __FUNCTION__, notification);
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地推送" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    } else {
        NSLog(@"用户点击进来的");
    }
}

#pragma mark - iOS10代理
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    //1. 处理通知
    NSDictionary * userInfo = notification.request.content.userInfo;
    NSLog(@"iOS10 willPresent 收到了推送%@", userInfo);
    //2. 处理完成后条用 completionHandler ，用于指示在前台显示通知的形式
    completionHandler(UNNotificationPresentationOptionAlert);

}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    NSLog(@"iOS10 didReceive 收到了推送%@", userInfo);
//    [self processJPush:userInfo];
    // 当应用在内存中存活时，收到推送会激发该回调，在这里来处理。
    // 后台时才会改变icon的数量，正在使用时不会。目前只需要清空icon数量标记。
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    completionHandler();  // 系统要求执行这个方法
}



#pragma mark - 全局页面属性设置
//全局设置导航栏的颜色，TabBar的颜色
- (void)setGlobalAppearance {
    //UINavigationBar
    [[UINavigationBar appearance] setBarTintColor:IAA_COLOR_0x129CFF(1.0)];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    NSDictionary *textAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                     NSFontAttributeName: [UIFont systemFontOfSize:18]};
    [[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
    
    //UIBarButtonItem
//    NSDictionary * barItemAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
//                                         NSFontAttributeName: [UIFont systemFontOfSize:14]};
    NSDictionary * barItemAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    [[UIBarButtonItem appearance] setTitleTextAttributes:barItemAttributes forState:UIControlStateNormal];
    
    //TabBar
    [[UITabBar appearance] setTintColor:IAA_COLOR_0x129CFF(1.0)];
    NSDictionary * itemAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:9]};
    [[UIBarItem appearance] setTitleTextAttributes:itemAttributes forState:UIControlStateNormal];
}

@end
